package com.cuit.house.controller;

import com.cuit.house.common.ResultMsg;
import com.cuit.house.model.User;
import com.cuit.house.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("sign01")
    public String home(){
        return "pageapp-signup";
    }
    @RequestMapping("login01")
    public String home1(){
        return "pageapp-login";
    }


    // 参数添加   @RequestBody
    /*---------------------------------注册-----------------------------------*/
    //激活key 验证码
    @RequestMapping("enable")
    public String enable(User user, String key){
        if(key.length() == 4){
            userService.enable(user, key);
            return "index";
        }
        return "pageapp-signup";
    }
    /*---------------------------------结束-----------------------------------*/


    /*---------------------------------登陆-----------------------------------*/
    @RequestMapping("login")
    public String login(User query)  {
        User user = userService.login(query);
        if(user!=null){
            return "index";
        }
        return "pageapp-login";
    }
    /*---------------------------------结束-----------------------------------*/

    /*---------------------------------修改密码-----------------------------------*/
    //修改密码
    @RequestMapping("change")
    public String change(User query,String key){
        userService.change(query,key);
        return "pageapp-login";
    }
    /*---------------------------------修改个人信息-----------------------------------*/
    @RequestMapping("changeAll")
    public String changeAll(User query){
        userService.changeAll(query);
        return "pageapp-login";
    }
    /*---------------------------------结束-----------------------------------*/

    //发送验证码
    @RequestMapping("send")
    public String send(User query, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //验证输入信息
        ResultMsg resultMsg = UserHelper.validate(query);
        userService.sendRandomKey(query);
        response.getWriter().print("200");
        return "100";
    }
}
